package org.crumbleworks.crumbchat.client.controller;

import javax.swing.JOptionPane;

import org.crumbleworks.crumbchat.client.gui.ChatFrame;
import org.crumbleworks.crumbchat.common.data.Message;

public class GuiController implements ClientControllerInterface {
    private ChatFrame chatFrame;
    private ClientControllerInterface controller;

    public GuiController(ClientControllerInterface clientController) {
        this.controller = clientController;

        chatFrame = new ChatFrame(this);
    }

    @Override
    public void connectToServer(String host, String username) {
        controller.connectToServer(host, username);
    }

    @Override
    public void disconnectFromServer(boolean shutdownAfterDisconnect) {
        controller.disconnectFromServer(shutdownAfterDisconnect);
    }

    @Override
    public void sendMessageToServer(String text) {
        controller.sendMessageToServer(text);
    }

    @Override
    public void putMessageToChat(Message message) {
        chatFrame.getChatPanel().putMessage(message.getUsername(), message.getMessage());
    }

    @Override
    public void showConnectError(String host) {
        JOptionPane.showMessageDialog(chatFrame.getLoginPanel(), "Konnte keine Verbindung zu '" + host + "' herstellen.",
                "Keine Verbindung", JOptionPane.INFORMATION_MESSAGE);
    }

    @Override
    public void shutdown() {
        chatFrame.dispose();
    }

    @Override
    public void parseInput(String input) {
        controller.parseInput(input);
    }
}
